package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.GraduateEntity;
import com.dengyafei.bean.ItemEntity;
import com.dengyafei.bean.PositionEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.mapper.GraduateEntityMapper;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.result.Result;
import com.dengyafei.service.RecommendService;
import com.dengyafei.utils.JwtUtil;
import com.dengyafei.utils.TypeParseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 邓亚非
 */
@Slf4j
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    RecommendService recommendService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    GraduateEntityMapper graduateEntityMapper;
    /**
     * 获取用户喜欢的物品:基于用户的协同过滤算法实现推荐
     *
     * @param id
     * @return
     */
    @JwtToken
    @PostMapping("/likes")
    public Result getUserLikes(Integer id) {
        Result result = new Result();
        List<ItemEntity> itemEntities = recommendService.recommendList(id);
        result.setCode(0);
        result.setMsg("推荐成功");
        result.setData(itemEntities);
        return result;
    }

    /**
     * 基于用户的协同过滤推荐接口
     * @return
     */
    @JwtToken
    @PostMapping(value = "/recommendBasedUser")
    public RestApiResult recommendBasedUser(){
        log.info("【推荐模块-基于用户协同过滤推荐接口-参数】:[{}]","无参");
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        GraduateEntity graduateEntity = graduateEntityMapper.queryAllByGname(userInfo);
        List<PositionEntity> recommendItemByUser = recommendService.getRecommendItemByUser(TypeParseUtil.integerParseLong(graduateEntity.getGid()), RecommendConstant.RECOMMEND_NUMBERS);
        log.info("【推荐模块-基于用户协同过滤推荐接口-成功】:[{}]", ResultMessage.Recommend.RECOMMEND_SUCCESS);
        return RestApiResult.ok(recommendItemByUser);
    }
}